using System.Collections;
using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;

public class ButtonNotifications : GenericDialog
{
	public Image imageSelf;

	public Text badgeLabel;

	public Sprite spriteBadgeOff;

	public Sprite spriteBadgeOn;

	private bool isActive;

	[HideInInspector]
	public bool hasBadge;

	private bool badgeRefreshing;

	private Tweener tweenMove;

	public override void Start()
	{
		base.Start();
		Economy.ActiveNotificationsChangedEvent += OnActiveNotificationsChanged;
	}

	private void OnDestroy()
	{
		Economy.ActiveNotificationsChangedEvent -= OnActiveNotificationsChanged;
	}

	private void OnActiveNotificationsChanged()
	{
		if (isActive && !badgeRefreshing)
		{
			RefreshBadge(0.3f);
		}
	}

	public void RefreshBadge()
	{
		int activeNotificationsCount = Economy.GetActiveNotificationsCount();
		if (activeNotificationsCount == 0)
		{
			hasBadge = false;
			imageSelf.sprite = spriteBadgeOff;
			badgeLabel.gameObject.SetActive(false);
		}
		else
		{
			hasBadge = true;
			imageSelf.sprite = spriteBadgeOn;
			badgeLabel.gameObject.SetActive(true);
			badgeLabel.text = activeNotificationsCount.ToString();
		}
		badgeRefreshing = false;
	}

	public void RefreshBadge(float delay)
	{
		badgeRefreshing = true;
		StartCoroutine(RefreshBadgeCo(delay));
	}

	private IEnumerator RefreshBadgeCo(float delay)
	{
		yield return new WaitForSeconds(delay);
		RefreshBadge();
	}

	public void HideBadge()
	{
		imageSelf.sprite = spriteBadgeOff;
		badgeLabel.gameObject.SetActive(false);
	}

	public void SlideIn()
	{
		if (tweenMove != null)
		{
			tweenMove.Kill();
			tweenMove = null;
		}
		tweenMove = base.rectTransform.DOAnchorPos(new Vector2(72f, -255f), 0.3f, true).SetEase(Ease.OutQuad).SetAutoKill(true)
			.OnComplete(delegate
			{
				isActive = true;
				RefreshBadge(0.1f);
			});
	}

	public void SlideOut()
	{
		if (tweenMove != null)
		{
			tweenMove.Kill();
			tweenMove = null;
		}
		isActive = false;
		tweenMove = base.rectTransform.DOAnchorPos(new Vector2(-180f, -255f), 0.1f, true).SetEase(Ease.InQuad).SetAutoKill(true);
	}
}
